#include "stdafx.h"

#include "pqdif_ph.h"
#include "pqdif_lg.h"
#include "pqdif_id.h"
#include "names.h"
#include "helpers.h"

#include <vector>
#include "AppStr.h"
using namespace std;

static const char SETTINGS_FILE[] = "txt_pqdif.cfg";

void InitPQDIFSupport (void)
{
	CString fileName (SETTINGS_FILE);

	InitNames();
}

void CleanupPQDIFSupport (void)
{
	CString fileName (SETTINGS_FILE);

}

// for a GUID value selector, search for tag names beginning with match_prefix
//  adds the tag name (stripping off match_prefix) to the combobox
//  sets the combobox item data to the GUID index

void FillGUIDCombo (CComboBox &cbo, const TCHAR *match_prefix)
{
	GUID tag;
	int imax = GetTagCount();
	int iskip = _tcslen (match_prefix);
	int idxComboBox;
	int idxTag;
	TCHAR *name;
	for (idxTag = 0; idxTag < imax; idxTag++) {
		APPSTR_ASCII ascii_Name(GetTagInfo (idxTag, tag));
		name = ascii_Name.WCHAR_PTR();
		if (StrStr (name, match_prefix)) {
			idxComboBox = cbo.AddString (name);// + iskip);
			cbo.SetItemData (idxComboBox, idxTag);
		}
	}
	cbo.SetCurSel (0);
}

// look at each cbo string, extract the tag index from cbo itemData
//   then if the tags match, select that string
BOOL SelectGUIDCombo (CComboBox &cbo,const GUID &tag,CString& buf,BOOL bSelect)
{
	GUID tagLookup;
	int idxComboBox;
	int imax = cbo.GetCount();
	int idxTag;
	//APPSTR_ASCII ascii_Buf(buf);
	for (idxComboBox = 0; idxComboBox < imax; idxComboBox++) {
		idxTag = cbo.GetItemData (idxComboBox);
		GetTagInfo (idxTag, tagLookup);
		if (PQDIF_IsEqualGUID (tag, tagLookup)) {
			if (bSelect) cbo.SetCurSel (idxComboBox);
			if (buf) cbo.GetLBText (idxComboBox,buf);
			return TRUE;
		}
	}
	return FALSE;
}

// for an Integer value selector, search for ID names beginning with match_prefix
//  adds the ID name (stripping off match_prefix) to the combobox
//  sets the combobox item data to the ID value (not index, as for a GUID selector)

void FillIntIDCombo (CComboBox &cbo, const TCHAR *match_prefix)
{
	int valueID;
	int imax = GetIntegerIDCount();
	int iskip = _tcslen (match_prefix);
	int idxComboBox;
	int idxID;
	TCHAR *name;

	for (idxID = 0; idxID < imax; idxID++) {
		APPSTR_ASCII ascii_Name(GetIntegerIDInfo (idxID, valueID));
		name = ascii_Name.WCHAR_PTR();
		if (StrStr (name, match_prefix)) {
			idxComboBox = cbo.AddString (name);// + iskip);
			cbo.SetItemData (idxComboBox, valueID);
		}
	}

	cbo.SetCurSel (0);
}

BOOL SelectIntIDCombo (CComboBox &cbo, unsigned long valueID,CString& buf,BOOL bSelect)
{
	UINT4 lookupID;
	int idxComboBox;
	int imax = cbo.GetCount();

	for (idxComboBox = 0; idxComboBox < imax; idxComboBox++) {
		lookupID = cbo.GetItemData (idxComboBox);
		if (valueID == lookupID) {
			if (bSelect) cbo.SetCurSel (idxComboBox);
			if (buf) cbo.GetLBText (idxComboBox, buf);
			return TRUE;
		}
	}
	return FALSE;
}

BOOL GetSelectedGUIDCombo (CComboBox &cbo, GUID &tag)
{
	int idx = cbo.GetCurSel();
	if (CB_ERR == idx) {
		return FALSE;
	}
	GetTagInfo (cbo.GetItemData (idx), tag);
	return TRUE;
}

BOOL GetSelectedIDCombo (CComboBox &cbo, unsigned long &valueID)
{
	int idx = cbo.GetCurSel();
	if (CB_ERR == idx) {
		return FALSE;
	}
	valueID = cbo.GetItemData (idx);
	return TRUE;
}

// find the tag by name
BOOL LookupGUIDCombo (CComboBox &cbo, const CString name, GUID &tag)
{
	int idx = cbo.FindString (-1, name);
	if (CB_ERR == idx) {
		return FALSE;
	}
	GetTagInfo (cbo.GetItemData (idx), tag);
	return TRUE;
}

// find the valueID by name
BOOL LookupIDCombo (CComboBox &cbo, const CString name, unsigned long &valueID)
{
	int idx = cbo.FindString (-1, name);
	if (CB_ERR == idx) {
		return FALSE;
	}
	valueID = cbo.GetItemData (idx);
	return TRUE;
}





// place tab stops so that the sub-strings line up nice
void SetListboxTabs (CListBox &lst)
{
	//vector<int> vColWidths;
	//int imax = lst.GetCount();
	//int i;
	//TCHAR buf [4096];

	//// find the column widths for dialog font by searching for \t, add a couple blanks padding
	//CDialog *pDlg = (CDialog *) lst.GetParent();
	//CDC *pDC = pDlg->GetDC();
	//CSize sz;
	//sz = pDC->GetTextExtent (_T("  "), 2);
	//int nPad = sz.cx;
	//for (i = 0; i < imax; ++i) {
	//	lst.GetText (i, buf);
	//	int idxCol = 0;
	//	char *tok = strtok(buf, "\t");
	//	while (tok) {
	//		sz = pDC->GetTextExtent (tok, strlen (tok));
	//		int nCol = sz.cx + nPad;
	//		if (vColWidths.size() <= idxCol) {
	//			vColWidths.push_back (nCol);
	//		} else if (nCol > vColWidths[idxCol]) {
	//			vColWidths[idxCol] = nCol;
	//		}
	//		++idxCol;
	//		tok = strtok (NULL, "\t");
	//	}
	//}
	//// calculate a scale factor from text width to dialog units
	//CRect rct;
	//rct.left = 0;
	//rct.right = 100;  // logical (pixel) width
	//rct.top = 0;
	//rct.bottom = 100;
	//pDlg->MapDialogRect (&rct);
	//pDlg->ReleaseDC (pDC);
	//double dScale = 1.0;
	//if (rct.right > 0) dScale = 100.0 / rct.right;

	//// convert column widths to tab settings in dialog units, set them into the list box
	//int nTabs = vColWidths.size() - 1;
	//if (nTabs > 0) {
	//	int *anTabs = new int[nTabs];
	//	int nStop = 0;
	//	for (i = 0; i < nTabs; ++i) {
	//		nStop += vColWidths[i];
	//		anTabs[i] = nStop * dScale;
	//	}
	//	lst.SetTabStops (nTabs, anTabs);
	//	delete [] anTabs;
	//}
}
